﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SharedComponents;
using System.Collections;
using System.Threading;

namespace OurLeague
{
    public partial class HomeForm : Form
    {
        public frmFrontEnd fFrontEnd;
        public frmBackEnd fBackEnd;
        public HomeForm()
        {
            InitializeComponent();
            fFrontEnd = null;
            fBackEnd = null;
        }

        private void HomeForm_Load(object sender, EventArgs e)
        {
            CheckForIllegalCrossThreadCalls = false;
            //pctBarLoading.Left = (-1) * pctBarLoading.Width;
            pctBarLoading.Visible = true;
        }

        private void pctFrontEndMenu_Click(object sender, EventArgs e)
        {
            if (fFrontEnd == null || fFrontEnd.IsDisposed)
                fFrontEnd = new frmFrontEnd(2);

            fFrontEnd.Show();
        }


        private void pctBackEndMenu_Click(object sender, EventArgs e)
        {
            Application.UseWaitCursor = true;
            if (fBackEnd == null || fBackEnd.IsDisposed)
                fBackEnd = new frmBackEnd();

            fBackEnd.Show();
        }

        private void tmrLoading_Tick(object sender, EventArgs e)
        {
            tmrBarLoading.Enabled = true;
            bool response = MySqlManager.performOpenConnection();
            tmrBarLoading.Enabled = false;
            tmrLoading.Enabled = false;
            Application.UseWaitCursor = false;
            //pctBarLoading.Left = 0;

            if (!response)
            {
                MessageBox.Show("Spiacente, ma è risultato impossibile connettersi al database '" + MySqlManager.Database + "'",
                                "Errore di connessione",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Warning);
                Application.Exit();
            }
            else
            {       
                pctFrontEndMenu.BackgroundImage = OurLeague.Properties.Resources.frontend_off;
                pctBackEndMenu.Image = OurLeague.Properties.Resources.backend_off;
                pctFrontEndMenu.Visible = true;
                pctBackEndMenu.Visible = true;
            }
        }

        private void HomeForm_Shown(object sender, EventArgs e)
        {
            tmrLoading.Enabled = true;
        }

        private void pctFrontEndMenu_MouseEnter(object sender, EventArgs e)
        {
            pctBall.Top = pctFrontEndMenu.Top + 10;
            pctBall.Left = pctFrontEndMenu.Left - pctBall.Width;

            pctFrontEndMenu.BackgroundImage = OurLeague.Properties.Resources.frontend_on;
            pctBall.Visible = true;
        }

        private void pctFrontEndMenu_MouseLeave(object sender, EventArgs e)
        {
            pctFrontEndMenu.BackgroundImage = OurLeague.Properties.Resources.frontend_off;
            pctBall.Visible = false;
        }

        private void pctBackEndMenu_MouseLeave(object sender, EventArgs e)
        {
            pctBackEndMenu.Image = OurLeague.Properties.Resources.backend_off;
            pctBall.Visible = false;
        }

        private void pctBackEndMenu_MouseEnter(object sender, EventArgs e)
        {
            pctBall.Top = pctBackEndMenu.Top + 10;
            pctBall.Left = pctBackEndMenu.Left - pctBall.Width;
            pctBackEndMenu.Image = OurLeague.Properties.Resources.backend_on;
            pctBall.Visible = true;
        }

        private void tmrBarLoading_Tick(object sender, EventArgs e)
        {
            for (int i = pctBarLoading.Left; i < 0; i++)
            {
                pctBarLoading.Left = i;
                pctBarLoading.Refresh();
            }
                
                if (pctBarLoading.Left == 0)
                    tmrBarLoading.Enabled = false;
        }
    }
}

